﻿using System;
using Rabbit.Modal; 

namespace Rabbit.BusinessLogicLayer
{
    public class UserBL
    {
        public static User CurrentUser = new User();
        public static User GetCurrentUserInformation()
        {
            string userJson = RequestHelper.SendRequest("/account/verify_credentials.json", "");
            UserSettings settings = CurrentUser.UserSettings;
            CurrentUser = WrappedJsonHelper.Deserialize<User>(userJson);
            if (settings != null)
            {
                CurrentUser.UserSettings = settings;
            }
            return CurrentUser;
        }

        //check whether a user follow me
        public static bool CheckIsFollowingMe(long userID)
        {
            string result = RequestHelper.SendRequest("friendships/exists.json", "user_a=" + userID + "&user_b=" + CurrentUser.Id);
            //due to sohu api returned bad structured json string, we need to convert it to a correct string
            result = result.Replace("{friends:", "{\"friends\":");
            return WrappedJsonHelper.Deserialize<UserRelationShip>(result).IsFriends;
        }
        //check whether a user follow me
        public static bool CheckIsFollowingMe(string userScreenName)
        {
            userScreenName = Utils.UrlEncoder.UrlEncode(userScreenName);
            string result = RequestHelper.SendRequest("friendships/exists.json", "screen_name_a=" + userScreenName + "&screen_name_a=" + CurrentUser.ScreenName);
            //due to sohu api returned bad structured json string, we need to convert it to a correct string
            result = result.Replace("{friends:", "{\"friends\":");
            return WrappedJsonHelper.Deserialize<UserRelationShip>(result).IsFriends;
        }

        public static User CreateFriendship(long userId)
        {
            string result = RequestHelper.SendRequest("/friendships/create/" + userId + ".json", "", "Post");
            return WrappedJsonHelper.Deserialize<User>(result);
        }

        public static User RemoveFriendship(long userId)
        {
            string result = RequestHelper.SendRequest("/friendships/destroy/" + userId + ".json", "", "Post");
            return WrappedJsonHelper.Deserialize<User>(result);
        }
    }
}
